﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Test.Ftt;

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            
            SearchAndDisplaySearchResults();
            this.detailPanel.Visible = false;

            Cursor.Current = Cursors.Default;
        }

        private void resultsTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag != null)
            {
                Cursor.Current = Cursors.WaitCursor;

                this.detailPanel.Visible = true;

                Int32 imdbId=Convert.ToInt32(e.Node.Tag);

                //Getting the details doesn't depend on the language
                GetFilmDetails(imdbId);

                if (this.languagesComboBox.SelectedIndex > 0)
                {
                    String languageIsoCode = this.languagesComboBox.SelectedValue.ToString();
                    GetTranslations(imdbId, languageIsoCode);
                    GetPlots(imdbId, languageIsoCode);
                }
                else
                {
                    GetTranslations(imdbId);
                    GetPlots(imdbId);

                }

                Cursor.Current = Cursors.Default;
            }
        }

        /// <summary>
        /// Search the film on Imdb + in the translation database
        /// </summary>
        private void SearchAndDisplaySearchResults()
        {
            using (Test.Ftt.FttServicesSoapClient webService = new Test.Ftt.FttServicesSoapClient())
            {
                Ftt.Films films;

                //Call the web service to perform the search
                if (this.languagesComboBox.SelectedIndex > 0)
                {
                    //if a language is selected, the imdb search is still the same (can't change anything there!), but the search in the Aka db is limited to the Aka for this isocode
                    films = new Ftt.FttServicesSoapClient().SearchFilmWithLanguageOption(this.textBox1.Text, (String)this.languagesComboBox.SelectedValue);
                }
                else
                {
                    films = new Ftt.FttServicesSoapClient().SearchFilm(this.textBox1.Text);
                    //It's also possible to use an enum although the list might be not complete:
                    //films = new Ftt.FttServicesSoapClient().SearchFilmWithLanguageOption(this.textBox1.Text, Ftt.LanguageEnum.fr.ToString());
                }

                //A bit of makeup
                this.recordsCountLabel.Visible = true;
                this.recordsCountLabel.Text =
                    (films.PopularTitles.Count() + films.Akas.Count()).ToString();
                resultsTreeView.Nodes.Clear();


                //Display the Popular Titles
                TreeNode TnP;
                if (films.PopularTitles != null && films.PopularTitles.Count() > 0)
                {
                    TnP = new TreeNode("Popular Titles");
                    foreach (Film film in films.PopularTitles)
                    {
                        TnP.Nodes.Add(new TreeNode()
                        {
                            Text = film.Title,
                            Tag = film.ImdbId
                        });
                    }
                    resultsTreeView.Nodes.Add(TnP);
                }


                //Display the Exact matches
                if (films.ExactMatches != null && films.ExactMatches.Count() > 0)
                {
                    TnP = new TreeNode("Exact matches");
                    foreach (Film film in films.ExactMatches)
                    {
                        TnP.Nodes.Add(new TreeNode()
                        {
                            Text = film.Title,
                            Tag = film.ImdbId
                        });
                    }
                    resultsTreeView.Nodes.Add(TnP);
                }

                //Display the Translation (Akas) matches
                if (films.Akas.Count() > 0)
                {
                    TnP = new TreeNode("Translations");
                    foreach (Aka film in films.Akas)
                    {
                        TnP.Nodes.Add(new TreeNode()
                        {
                            Text = film.Title,
                            Tag = film.ImdbId
                        });
                    }
                    resultsTreeView.Nodes.Add(TnP);
                }
            }
        }

        private void GetFilmDetails(Int32 imdbId)
        {
            using (Test.Ftt.FttServicesSoapClient webService = new Test.Ftt.FttServicesSoapClient())
            {
                Test.Ftt.Film film = webService.GetFilmDetails(imdbId);

                this.titleLabel.Visible = true;
                this.titleLabel.Text = film.Title;

                this.yearLabel.Visible = true;
                this.yearLabel.Text = film.Year.ToString();

                this.tvFilmLabel.Visible = true;
                this.tvFilmLabel.Text = (film.TvFilm == true) ? "Yes" : "No";

                this.directorLabel.Visible = true;
                this.directorLabel.Text = film.Directors[0].Name;
            }
        }

        private void GetTranslations(Int32 imdbId)
        {
            using (Test.Ftt.FttServicesSoapClient webService = new Test.Ftt.FttServicesSoapClient())
            {
                this.translationsGridView.AutoGenerateColumns = false;
                this.translationsGridView.DataSource = webService.GetTitleTranslations(imdbId);
            }

        }

        private void GetTranslations(Int32 imdbId, String languageIsoCode)
        {
            using (Test.Ftt.FttServicesSoapClient webService = new Test.Ftt.FttServicesSoapClient())
            {
                this.translationsGridView.AutoGenerateColumns = false;
                this.translationsGridView.DataSource = webService.GetTitleTranslationsWithLanguageOption(imdbId, languageIsoCode);
            }

        }


        private void GetPlots(Int32 imdbId)
        {
            using (Test.Ftt.FttServicesSoapClient webService = new Test.Ftt.FttServicesSoapClient())
            {
                this.plotsGridView.AutoGenerateColumns = false;
                this.plotsGridView.DataSource = webService.GetTitlePlots(imdbId);
            }

        }

        private void GetPlots(Int32 imdbId, String languageIsoCode)
        {
            using (Test.Ftt.FttServicesSoapClient webService = new Test.Ftt.FttServicesSoapClient())
            {
                this.plotsGridView.AutoGenerateColumns = false;
                this.plotsGridView.DataSource = webService.GetTitlePlotsWithLanguageOption(imdbId, languageIsoCode);
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            using (Test.Ftt.FttServicesSoapClient webService = new Test.Ftt.FttServicesSoapClient())
            {
                this.languagesComboBox.DataSource = webService.GetLanguages();

                this.languagesComboBox.DisplayMember = "LanguageName";
                this.languagesComboBox.ValueMember = "LanguageIsoCode";

                this.languagesComboBox.SelectedIndex = -1;
            }
        }
    }
}
